import 'dart:convert';

import 'package:flutter_app/bean/app_set_info_entity.dart';
import 'package:flutter_app/utils/sp_utils.dart';
import 'package:flutter_app/values/CommonParam.dart';
import 'package:get/get.dart';

///
/// App 配置管理
///
class AppSetInfoManager {
  // 私有构造函数
  AppSetInfoManager._internal();

  // 保存单例
  static final AppSetInfoManager _appDirectoryManager = AppSetInfoManager._internal();

  // 工厂构造函数
  factory AppSetInfoManager() => _appDirectoryManager;

  AppSetInfoEntity? appSetInfoEntity;

  void setAppSetInfoEntity(AppSetInfoEntity? appSetInfoEntity) {
    this.appSetInfoEntity = appSetInfoEntity;
    sp.setString(CommonParam.APP_DIRECTORY, jsonEncode(appSetInfoEntity));
  }

  // 获取配置信息
  Future<AppSetInfoEntity?> getAppSetInfoEntity() async {
    if (appSetInfoEntity == null) {
      sp.getString(CommonParam.APP_DIRECTORY).then((value) {
        if (value != null) {
          appSetInfoEntity = AppSetInfoEntity.fromJson(jsonDecode(value));
        }
      });
    }
    return appSetInfoEntity;
  }

  // 获取用户协议和隐私政策
  String getAgreementUrl() {
    // return appSetInfoEntity?.agreement ?? "http://www.xnah.gov.cn/privacypolicy.html";
    if (GetUtils.isNullOrBlank(appSetInfoEntity?.agreement) ?? false) {
      return "https://xxx.szzfyjzx.icu:5236/publicPortal_organs/privacypolicy.html";
    }
    return appSetInfoEntity!.agreement!;
  }

  // 获取应用权限说明
  String getPermissionUrl() {
    // return appSetInfoEntity?.permission ?? "http://www.xnah.gov.cn/phonepermission.html";
    if (GetUtils.isNullOrBlank(appSetInfoEntity?.permission) ?? false) {
      return "https://xxx.szzfyjzx.icu:5236/publicPortal_organs/phonepermission.html";
    }
    return appSetInfoEntity!.permission!;
  }

  // 获取积分说明
  String getAccumulateUrl() {
    // return appSetInfoEntity?.integral ?? "http://www.xnah.gov.cn/inspectorPoints.html";
    if (GetUtils.isNullOrBlank(appSetInfoEntity?.integral) ?? false) {
      return "https://xxx.szzfyjzx.icu:5236/publicPortal_organs/inspectorPoints.html";
    }
    return appSetInfoEntity!.integral!;
  }

  // 获取随手拍说明
  String getFilmUrl() {
    // return appSetInfoEntity?.film ?? "http://www.xnah.gov.cn/reportNotices.html";
    if (GetUtils.isNullOrBlank(appSetInfoEntity?.film) ?? false) {
      return "https://xxx.szzfyjzx.icu:5236/publicPortal_organs/reportNotices.html";
    }
    return appSetInfoEntity!.film!;
  }

  // 获取pinState 默认0 不开启
  String getPinState() {
    return appSetInfoEntity?.pinState ?? "0";
  }

  // 移除 隐私协议弹窗 标识
  // 隐私协议更新 需要二次弹出时调用
  void removeFirstLogin() {
    sp.remove(CommonParam.FIRST_LOGIN);
  }
}
